//
//  CustomClassTableViewCell.m
//  ChunLaiZhuangClient
//
//  Created by zouzhisheng on 16/3/2.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "CustomClassTableViewCell.h"
#import "CategoryListViewController.h"
@implementation CustomClassTableViewCell
+ (instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *identifier = @"status";
        // 1.缓存中取
        CustomClassTableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        // 2.创建
         if (cell == nil) {
                 cell = [[CustomClassTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
             }
          return cell;
}
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor=[UIColor colorWithHexString:@"efeff4"];
        _bgView=[[UIView alloc] initWithFrame:CGRectMake(4,4, SCREEN_WIDTH-8, 105)];
        _bgView.backgroundColor=[UIColor whiteColor];
        _bgImageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH-8, 105)];
        _bgImageView.layer.borderColor=[UIColor colorWithHexString:@"d8d8d8"].CGColor;
        _bgImageView.layer.borderWidth=1;
        _bgImageView.contentMode=UIViewContentModeScaleAspectFill;
        _bgImageView.clipsToBounds=YES;
        _bgImageView.userInteractionEnabled=YES;
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [_bgImageView addGestureRecognizer:tap];
        [self addSubview:_bgImageView];
        _nameLable=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 230/2,45)];
        _nameLable.textAlignment=NSTextAlignmentCenter;
        _nameLable.backgroundColor=[UIColor clearColor];
        _nameLable.center=CGPointMake(_bgImageView.center.x, _bgImageView.height/2);
        _nameLable.textColor=[UIColor colorWithHexString:@"ffffff"];
        _nameLable.font=[UIFont boldSystemFontOfSize:22];
        [_bgView addSubview:_bgImageView];
        [_bgView addSubview:_nameLable];
        [self addSubview:_bgView];
        UIImageView *leftImage=[[UIImageView alloc] initWithFrame:CGRectMake(_nameLable.left-10,_bgImageView.height/2-3.5, 7,8)];
        leftImage.image=[UIImage imageNamed:@"icon_cla_left"];
        [_bgView addSubview:leftImage];
        
        UIImageView *rigImage=[[UIImageView alloc] initWithFrame:CGRectMake(_nameLable.right+10-8,_bgImageView.height/2-3.5, 7,8)];
        rigImage.image=[UIImage imageNamed:@"icon_cla_rig"];
        [_bgView addSubview:rigImage];
        
        UILabel *lookLaebl=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80,19)];
        lookLaebl.center=CGPointMake(SCREEN_WIDTH/2, 0);
        lookLaebl.top=_nameLable.bottom-5;
        lookLaebl.text=@"查看全部";
        lookLaebl.textAlignment=NSTextAlignmentCenter;
        lookLaebl.font=[UIFont systemFontOfSize:12];
        lookLaebl.textColor=[UIColor whiteColor];
        lookLaebl.layer.cornerRadius=10;
        lookLaebl.layer.borderWidth=1;
        lookLaebl.layer.borderColor=[[UIColor whiteColor] colorWithAlphaComponent:0.7].CGColor;
        [_bgView addSubview:lookLaebl];
        
        
    }
    return self;
}
-(void)setFrameModel:(ClassFrameModel *)frameModel{
    _frameModel=frameModel;
    if (!self.buttonsArray) {
        self.buttonsArray=[[NSMutableArray alloc] init];
    }else{
        if (self.buttonsArray.count>0) {
            for(UIButton *bt in self.buttonsArray){
                [bt removeFromSuperview];
            }
        }
    }
     _bgView.height=frameModel.cellHeight-8;
    [_bgImageView sd_setImageWithURL:[NSURL URLWithString:frameModel.dataModel.imageViewUrl] placeholderImage:nil];
    [self setLabelSpace:_nameLable withValue:frameModel.dataModel.title withFont:[UIFont boldSystemFontOfSize:22]];
    float edge=14.0;
    float widh=70;
    float distance=(SCREEN_WIDTH-14*2-widh*4)/3;
    for (int i=0; i<frameModel.dataModel.classArray.count; i++) {
        SubcategoryModel *model=frameModel.dataModel.classArray[i];
        UIButton *tagBt=[UIButton buttonWithType:UIButtonTypeCustom];
        tagBt.frame=CGRectMake(0, 0, widh, 20);
        tagBt.left=edge+(widh+distance)*(i%4);
        tagBt.top=_bgImageView.height+15+(tagBt.height+15)*(i/4);
        tagBt.tag=i;
        tagBt.titleLabel.font=[UIFont systemFontOfSize:14];
        if (model.color) {
          [tagBt setTitleColor:[UIColor colorWithHexString:model.color] forState:UIControlStateNormal];
        }else{
          [tagBt setTitleColor:[UIColor colorWithHexString:@"222222"] forState:UIControlStateNormal];
        }
        [tagBt setTitle:model.name forState:UIControlStateNormal];
        [_bgView addSubview:tagBt];
        [tagBt addTarget:self action:@selector(tagAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.buttonsArray addObject:tagBt];
    }
}

-(void)setLabelSpace:(UILabel*)label withValue:(NSString*)str withFont:(UIFont*)font {
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    //设置字间距 NSKernAttributeName:@1.5f
    NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@8.0f
                          };
    
    NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:str attributes:dic];
    label.attributedText = attributeStr;
}

-(void)tapAction:(UITapGestureRecognizer*)tap{
    UIViewController *currentVC=[CustomClassTableViewCell viewController:self];
    CategoryListViewController *categoryVC=[[CategoryListViewController alloc] init];
    [categoryVC setDataModel:_frameModel.dataModel];
    categoryVC.navTitle=_frameModel.dataModel.title;
    [currentVC.navigationController pushViewController:categoryVC animated:YES];
}
#pragma mark-标签方法
-(void)tagAction:(UIButton*)bt{
    UIViewController *currentVC=[CustomClassTableViewCell viewController:self];
    CategoryListViewController *categoryVC=[[CategoryListViewController alloc] init];
    SubcategoryModel *subModel=_frameModel.dataModel.classArray[bt.tag];
    [categoryVC setDataModel:_frameModel.dataModel];
    categoryVC.subcategoryModel=subModel;
    categoryVC.navTitle=subModel.name;
    [currentVC.navigationController pushViewController:categoryVC animated:YES];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
